from PySide6.QtWidgets import QWidget, QTableWidgetItem, QHBoxLayout

from common.player.reference import Player
from gui.signals import SIGNALS
from gui.widgets.double_col_view import DoubleColView


class PlayerPanelWidget(QWidget):
    def __init__(self, parent, player: Player):
        super().__init__(parent)

        self.player = player

        self.left_table = DoubleColView(['等级', '经验', '金币', '生命', '法力', '集气'])
        self.update_left_table()
        self.right_table = DoubleColView(['攻击', '防御', '力量', '闪避', '承伤', '属性'])
        self.update_right_table()

        panel_layout = QHBoxLayout(self)
        panel_layout.setContentsMargins(0, 0, 0, 0)
        panel_layout.setSpacing(0)

        panel_layout.addWidget(self.left_table, 35)
        panel_layout.addWidget(self.right_table, 65)

        SIGNALS.update_player_penal.emit()

    def update_left_table(self):
        rows = [
            self.player.state.level,
            f'{self.player.state.exp} ({self.player.state.exp.rate()})',
            self.player.bag.coin,
            self.player.state.hp,
            self.player.state.mp,
            self.player.state.qi_rate,
        ]
        for i, row in enumerate(rows):
            if isinstance(row, str):
                self.left_table.setItem(i, 1, QTableWidgetItem(row))
            else:
                self.left_table.setItem(i, 1, QTableWidgetItem(str(row)))

    def update_right_table(self):
        rows = [
            self.player.attack(),
            self.player.state.defense,
            self.player.state.strength,
            f'{self.player.state.dodge}%',
            f'{self.player.state.take_damage}%',
            self.player.state.attribute.values,
            ''
        ]
        for i, row in enumerate(rows):
            if isinstance(row, str):
                self.right_table.setItem(i, 1, QTableWidgetItem(row))
            else:
                self.right_table.setItem(i, 1, QTableWidgetItem(str(row)))

    def update_data(self):
        self.update_left_table()
        self.update_right_table()
